package com.zmn.oms.zmn.business.impl.redistribute;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.zmn.base.engineer.dubbo.interfaces.supervisor.EngineerSupervisorListRemoteService;
import com.zmn.biz.abnormal.common.dio.AbnorAbnormalExtendDIO;
import com.zmn.biz.abnormal.dubbo.interfaces.AbnormalModifyRemoteService;
import com.zmn.biz.complain.common.constant.UmsStaffConsts;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.impl.messageV1.ZmnMcsSendUtil;
import com.zmn.oms.business.interfaces.messageV1.common.MessageSendBaseService;
import com.zmn.oms.common.annotation.OrderWorkRedistributeLog;
import com.zmn.oms.common.constant.ConfDistributeReasonConsts;
import com.zmn.oms.common.constant.ConfRedistributeConsts;
import com.zmn.oms.common.constant.MessageConsts;
import com.zmn.oms.common.constant.OrderWorkRedistributeLogConsts;
import com.zmn.oms.common.enums.RedistributeAuditStatusEnum;
import com.zmn.oms.common.enums.RedistributeStatusEnum;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.work.redistribute.OrderWorkRedistributeApplyBO;
import com.zmn.oms.model.bo.work.redistribute.OrderWorkRedistributeAuditBO;
import com.zmn.oms.model.bo.work.redistribute.RedistributeMasterBO;
import com.zmn.oms.model.dto.messageV1.AppMessageDTO;
import com.zmn.oms.model.dto.messageV1.DingTalkMessageDIO;
import com.zmn.oms.model.dto.messageV1.SendMscDIO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalUpdateOrderDTO;
import com.zmn.oms.model.dto.product.OrderProductDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.masterworking.MasterWorking;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.redistribute.OrderWorkRedistributeApply;
import com.zmn.oms.model.entity.redistribute.OrderWorkRedistributeInfo;
import com.zmn.oms.model.entity.redistribute.OrderWorkRedistributeMaster;
import com.zmn.oms.model.entity.work.DistributeMasterQuery;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.ZsOrderDistributeVO;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.masterworking.MasterWorkingService;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeApplyService;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeInfoService;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeMasterService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeAuditBService;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeBService;
import com.zmn.oms.zmn.business.interfaces.work.customwork.ZsDistributeWorkBService;
import com.zmn.oms.zmn.normal.business.interfaces.order.ZsNormalOrderBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述: 改派申请审核业务类 (为了记录日志抽取出来的类)
 *
 * @author: HuangChao
 * @since: 2022/05/17 17:30
 */
@Slf4j
@Service
public class OrderWorkRedistributeAuditBServiceImpl implements OrderWorkRedistributeAuditBService {

    @Autowired
    private OrderWorkRedistributeApplyService orderWorkRedistributeApplyService;
    @Autowired
    private OrderWorkRedistributeInfoService orderWorkRedistributeInfoService;
    @Autowired
    private OrderWorkRedistributeMasterService orderWorkRedistributeMasterService;
    @Autowired
    private MasterWorkingService masterWorkingService;
    @Autowired
    private OrderWorkRedistributeBService orderWorkRedistributeBService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private ZsDistributeWorkBService zsDistributeWorkBService;
    @Autowired
    private MessageSendBaseService messageSendBaseService;
    @Autowired
    private OrderProductService orderProductService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ZsNormalOrderBService zsNormalOrderBService;

    @DubboReference(version = com.zmn.biz.abnormal.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private AbnormalModifyRemoteService abnormalModifyRemoteService;
    @DubboReference(version = com.zmn.base.engineer.common.constants.DubboConsts.INTERFACE_VERSION, check = false)
    private EngineerSupervisorListRemoteService engineerSupervisorListRemoteService;
    // 虚假改派异常内容id 测试ID：1164；正式ID：1245
    @Value("${ums.exception.content-id.spurious_redistribute_exception:1164}")
    private Integer SPURIOUS_REDISTRIBUTE_EXCEPTION_CONTENT_ID;


    @Override
    @Transactional(rollbackFor = Exception.class)
    @OrderWorkRedistributeLog(type = OrderWorkRedistributeLogConsts.ORDER_REDISTRIBUTE_LOG_TYPE_START_AUDIT, beforeProceed = false)
    public void redistributeAuditStart(OrderWorkRedistributeAuditBO auditBO) {
        log.info("开始审核处理-参数：{}", JSON.toJSONString(auditBO));
        Objects.requireNonNull(auditBO.getRedistributeId(), "改派申请Id不能为空");
        auditBO.setOperateTime(DateUtil.addSecond(auditBO.getOperateTime(), 30));
        OrderWorkRedistributeApply orderWorkRedistributeApply = new OrderWorkRedistributeApply();
        orderWorkRedistributeApply.setRedistributeId(auditBO.getRedistributeId());
        orderWorkRedistributeApply.setAuditStatus(RedistributeAuditStatusEnum.WAIT_AUDIT.getCode());
        orderWorkRedistributeApply.setUpdateTime(DateUtil.getNow());
        String logContent = "待员工审核";
        if (Objects.equals(auditBO.getAuditType(), ConfRedistributeConsts.AUDIT_TYPE_MANAGER)) {
            // 工程师主管审核 发送钉钉消息
            Boolean flag = this.sendDingTalkMessage(auditBO);
            if (!flag) {
                // 工程师主管审核发送钉钉消息失败
                orderWorkRedistributeApply.setAuditType(ConfRedistributeConsts.AUDIT_TYPE_STAFF);
                // 日志处理使用
                auditBO.setAuditType(ConfRedistributeConsts.AUDIT_TYPE_STAFF);
            } else {
                logContent = "待" + auditBO.getMasterSupervisorName() + "工程师主管审核";
            }
        }
        orderWorkRedistributeApplyService.updateByPrimaryKeySelective(orderWorkRedistributeApply);

        // 设置日志参数
        auditBO.setRedistributeOperatorLogRemark(logContent);
        auditBO.setOperateTime(DateUtil.addMillisecond(DateUtil.getNow(), 500));
    }

    @Override
    @OrderWorkRedistributeLog(type = OrderWorkRedistributeLogConsts.ORDER_REDISTRIBUTE_LOG_TYPE_ADD_EXCEPTION, beforeProceed = false)
    public void saveSpuriousRedistributeException(OrderWorkRedistributeAuditBO auditBO, OrderWorkRedistributeApply orderWorkRedistributeApply) throws OmsBaseException {
        OrderWorkRedistributeInfo info = orderWorkRedistributeInfoService.findRedistributeInfoById(auditBO.getRedistributeId());
        AbnorAbnormalExtendDIO abnorAbnormalExtendDIO = BeanMapper.map(auditBO, AbnorAbnormalExtendDIO.class);
        abnorAbnormalExtendDIO.setStaffType(UmsStaffConsts.STAFF_TYPE_MASTER);
        abnorAbnormalExtendDIO.setAbnormalStaffId(info.getMasterId().intValue());
        abnorAbnormalExtendDIO.setContentIdList(Collections.singletonList(SPURIOUS_REDISTRIBUTE_EXCEPTION_CONTENT_ID));
        abnorAbnormalExtendDIO.setRemark(auditBO.getAuditRemark());
        ResponseDTO responseDTO = abnormalModifyRemoteService.addAbnormal(abnorAbnormalExtendDIO);
        log.info("#保存虚假改派异常，入参：{}-> 出参：{}", JSON.toJSONString(abnorAbnormalExtendDIO), JSON.toJSONString(responseDTO));
        if (!responseDTO.isSuccess()) {
            throw new OmsBaseException(responseDTO.getMessage());
        }
        // 设置日志参数
        auditBO.setOperateTime(DateUtil.getNow());
    }

    @Override
    @OrderWorkRedistributeLog(type = OrderWorkRedistributeLogConsts.ORDER_REDISTRIBUTE_LOG_TYPE_AUDIT, beforeProceed = false)
    @Transactional(rollbackFor = Exception.class)
    public OrderWorkRedistributeApply redistributeAudit(OrderWorkRedistributeAuditBO auditBO, OrderWorkRedistributeApply orderWorkRedistributeApply) throws OmsBaseException {
        // 1、校验
        if (Objects.isNull(orderWorkRedistributeApply)
                || !Objects.equals(orderWorkRedistributeApply.getRedistributeStatus(), RedistributeStatusEnum.WAIT_REDISTRIBUTE.getCode())) {
            throw new OmsBaseException("该改派申请不存在或已完结！");
        }

        if (!(Objects.equals(orderWorkRedistributeApply.getAuditStatus(), RedistributeAuditStatusEnum.WAIT_AUDIT.getCode())
                || Objects.equals(orderWorkRedistributeApply.getAuditStatus(), RedistributeAuditStatusEnum.PROCESSING_AUDIT.getCode()))) {
            throw new OmsBaseException("该改派申请已审核或无需审核！");
        }

        // 2、根据审核状态进行处理
        if (StringUtils.isBlank(auditBO.getProductInfo())) {
            auditBO.setProductInfo(orderWorkRedistributeApply.getProductInfo());
        }

        auditBO.setApplyUserId(orderWorkRedistributeApply.getApplyUserId());
        auditBO.setApplyUser(orderWorkRedistributeApply.getApplyUser());
        auditBO.setApplyUserType(orderWorkRedistributeApply.getApplyUserType());
        BeanMapper.copy(auditBO, orderWorkRedistributeApply);
        orderWorkRedistributeApply.setAuditStatus(auditBO.getAuditStatus());
        // 3、审核失败操作
        if (Objects.equals(auditBO.getAuditStatus(), RedistributeAuditStatusEnum.AUDIT_REJECT.getCode())) {
            orderWorkRedistributeApply.setRedistributeStatus(RedistributeStatusEnum.REDISTRIBUTE_FAIL.getCode());

            // 通知工单进行中表 工单处于改派完毕状态
            MasterWorking masterWorking = new MasterWorking();
            masterWorking.setWorkId(orderWorkRedistributeApply.getWorkId());
            masterWorking.setRedistributeStatus(GlobalConsts.YES);
            masterWorkingService.updateRedistributeStatusByKey(masterWorking);

            // 发送改派失败站内信通知工程师改派失败
            OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderWorkRedistributeApply.getOrderId(), orderWorkRedistributeApply.getWorkId());
            StringBuilder failCause = new StringBuilder();
            failCause.append("失败原因：");
            failCause.append(StringUtils.isNotEmpty(auditBO.getAuditRemark()) ? auditBO.getAuditRemark() : "审核未通过");
            if (Objects.equals(auditBO.getSpuriousStatus(), GlobalConsts.YES)) {
                failCause.append("，被定义为虚假改派，影响近两日新单接单量");
            }

            // 向原工程师发送改派结果
            orderWorkRedistributeBService.pushRedistributeResultAppMsg(orderWorkRedistributeApply, null, orderWork, failCause.toString());
        } else {
            // 这里审核通过就设置为改派成功是为了防止特缘由导致的派单撤回 导致本条改派记录的撤回
            orderWorkRedistributeApply.setRedistributeStatus(RedistributeStatusEnum.REDISTRIBUTE_SUCCESS.getCode());

            // 部分缘由特殊处理
            if (Objects.equals(auditBO.getAuditStatus(), RedistributeAuditStatusEnum.AUDIT_PASS.getCode())
                    && (Objects.equals(orderWorkRedistributeApply.getReasonId(), ConfDistributeReasonConsts.REASON_TYPE_CATEGORY_WRONG)
                    || Objects.equals(orderWorkRedistributeApply.getReasonId(), ConfDistributeReasonConsts.REASON_TYPE_ADDRESS_WRONG))) {
                // 更新工单数据
                this.updateOrderWorkAddressOrProduct(orderWorkRedistributeApply, auditBO.getStreetSource());
            }
        }

        // 避免加密问题复制的对象
        OrderWorkRedistributeApply finalOrderWorkRedistributeApply = BeanMapper.map(orderWorkRedistributeApply, OrderWorkRedistributeApply.class);
        // 4、保存审核数据 同时将对应审核记录工程师数据置为失效
        orderWorkRedistributeApply.setUpdater(auditBO.getOperator());
        orderWorkRedistributeApply.setUpdateTime(DateUtil.getNow());
        orderWorkRedistributeApplyService.updateByPrimaryKeySelective(orderWorkRedistributeApply);
        orderWorkRedistributeMasterService.updateStatusByRedistributeId(auditBO.getRedistributeId(), GlobalConsts.NO);
        // 设置审核日志参数
        this.setAuditLogParams(auditBO, finalOrderWorkRedistributeApply);
        return finalOrderWorkRedistributeApply;
    }

    @Override
    public void redistributeHelpHandle(OrderWorkRedistributeApplyBO redistributeApplyBO, OrderWork orderWork, OrderDetail orderDetail) {
        log.info("转单求助-{}", redistributeApplyBO);
        try {
            List<RedistributeMasterBO> redistributeMasterList = Lists.newArrayList();
            switch (redistributeApplyBO.getHelpType()) {
                    case ConfRedistributeConsts.HELP_TYPE_SPECIFY:
                    if (Objects.nonNull(redistributeApplyBO.getHelpMasterId())) {
                        RedistributeMasterBO redistributeMasterBO = new RedistributeMasterBO();
                        redistributeMasterBO.setMasterId(redistributeApplyBO.getHelpMasterId());
                        redistributeMasterBO.setMasterName(redistributeApplyBO.getHelpMasterName());
                        redistributeMasterBO.setDistributeGrid(redistributeApplyBO.getDistributeGrid());
                        redistributeMasterList.add(redistributeMasterBO);
                    }
                    break;
                case ConfRedistributeConsts.HELP_TYPE_RANGE:
                    // 调用获取派单接口获取工程师集合
                    DistributeMasterQuery distributeMasterQuery = zsDistributeWorkBService.buildDistributeMasterQuery(orderWork, orderDetail, true);
                    if (Objects.equals(redistributeApplyBO.getReasonId(), ConfDistributeReasonConsts.REASON_TYPE_CATEGORY_WRONG)
                            && !Objects.equals(redistributeApplyBO.getProductId(), orderWork.getProductId())) {
                        distributeMasterQuery.setProductId(redistributeApplyBO.getProductId());
                        distributeMasterQuery.setServCategId(redistributeApplyBO.getServCategId());
                    }
                    Boolean isAddressChanged = Boolean.FALSE;
                    if (Objects.nonNull(redistributeApplyBO.getLatitude()) && Objects.nonNull(redistributeApplyBO.getLongitude())) {
                        if (!Objects.equals(orderDetail.getLatitude(), redistributeApplyBO.getLatitude()) || !Objects.equals(orderDetail.getLongitude(), redistributeApplyBO.getLongitude())) {
                            isAddressChanged = Boolean.TRUE;
                        }
                    }

                    if (isAddressChanged) {
                        distributeMasterQuery.setLatitude(redistributeApplyBO.getLatitude());
                        distributeMasterQuery.setLongitude(redistributeApplyBO.getLongitude());
                        distributeMasterQuery.setCountyId(redistributeApplyBO.getCountyId());
                        distributeMasterQuery.setStreetId(redistributeApplyBO.getStreetId());
                    }
                    List<ZsOrderDistributeVO> zsOrderDistributeVOList = zsDistributeWorkBService.getRedistributeMasterList(distributeMasterQuery, orderWork, orderDetail);
                    if (CollectionUtil.isNotNullOrEmpty(zsOrderDistributeVOList)) {
                        redistributeMasterList = zsOrderDistributeVOList.stream()
                                .map(m -> BeanMapper.map(m, RedistributeMasterBO.class))
                                .filter(r -> !Objects.equals(r.getMasterId(), orderWork.getMasterId()))
                                .collect(Collectors.toList());
                    }
                    break;
                default:
                    break;
            }

            // 5、通知工程师有转单求助 - 站内信
            if (CollectionUtil.isNotNullOrEmpty(redistributeMasterList)) {
                List<OrderWorkRedistributeMaster> orderWorkRedistributeMasterList = redistributeMasterList.stream().map(redistributeMasterBO -> {
                    OrderWorkRedistributeMaster orderWorkRedistributeMaster = BeanMapper.map(redistributeApplyBO, OrderWorkRedistributeMaster.class);
                    BeanMapper.copy(redistributeMasterBO, orderWorkRedistributeMaster);
                    orderWorkRedistributeMaster.setRedistributeId(redistributeApplyBO.getRedistributeId());
                    orderWorkRedistributeMaster.setStatus(GlobalConsts.YES);
                    orderWorkRedistributeMaster.setCreater("系统");
                    orderWorkRedistributeMaster.setCreateTime(DateUtil.getNow());
                    orderWorkRedistributeMaster.setUpdater("系统");
                    orderWorkRedistributeMaster.setUpdateTime(DateUtil.getNow());
                    return orderWorkRedistributeMaster;
                }).collect(Collectors.toList());

                // 保存可领单工程师数据
                orderWorkRedistributeMasterService.batchInsert(orderWorkRedistributeMasterList);

                // 通知工程师有转单求助
                this.pushRedistributeHelpAppMsg(redistributeMasterList, redistributeApplyBO, orderWork, orderDetail);
            }
        } catch (Exception e) {
            log.error("改派求助处理失败:{}", e);
        }
    }

    /**
     * 更新工单地址或商品信息
     *
     * @param orderWorkRedistributeApply
     */
    private void updateOrderWorkAddressOrProduct(OrderWorkRedistributeApply orderWorkRedistributeApply, Integer streetSource) {
        try {
            OrderWork dbOrderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderWorkRedistributeApply.getOrderId(), orderWorkRedistributeApply.getWorkId());
            OrderDetail orderDetail = orderDetailService.findOrderDetailByKeySrcMaster(orderWorkRedistributeApply.getOrderId());
            // 判断商品或地址是否修改过
            Boolean isProductChanged = Boolean.FALSE;
            if (NumberUtil.isNotNullOrZero(orderWorkRedistributeApply.getProductId())) {
                if (!Objects.equals(orderWorkRedistributeApply.getProductId(), dbOrderWork.getProductId())) {
                    isProductChanged = Boolean.TRUE;
                }
            }

            Boolean isAddressChanged = Boolean.FALSE;
            if (Objects.nonNull(orderWorkRedistributeApply.getLatitude()) && Objects.nonNull(orderWorkRedistributeApply.getLongitude())) {
                if (!Objects.equals(orderDetail.getLatitude(), orderWorkRedistributeApply.getLatitude()) || !Objects.equals(orderDetail.getLongitude(), orderWorkRedistributeApply.getLongitude())) {
                    isAddressChanged = Boolean.TRUE;
                }

                if (Objects.nonNull(orderWorkRedistributeApply.getAddress()) && !Objects.equals(orderDetail.getAddress(), orderWorkRedistributeApply.getAddress())) {
                    isAddressChanged = Boolean.TRUE;
                }
            }

            if (NumberUtil.isNullOrZero(orderWorkRedistributeApply.getCountyId())) {
                isAddressChanged = Boolean.FALSE;
            }

            // 判断是否需要更新订单相关信息
            if (!isProductChanged && !isAddressChanged) {
                return;
            }

            // 组装更新工单信息参数
            ZsNormalUpdateOrderDTO updateOrderDTO = BeanMapper.map(dbOrderWork, ZsNormalUpdateOrderDTO.class);
            BeanMapper.copy(orderDetail, updateOrderDTO);
            updateOrderDTO.setCityId(dbOrderWork.getCityId());
            updateOrderDTO.setVerifyAssignBack(Boolean.FALSE);
            updateOrderDTO.setVerifyDistributeBack(Boolean.FALSE);
            OrderProduct product = orderProductService.listOrderProductByOrderId(orderWorkRedistributeApply.getOrderId()).get(0);
            if (isProductChanged) {
                List<OrderProductDTO> orderProductList = Lists.newArrayList();
                OrderProductDTO orderProductDTO = new OrderProductDTO();
                orderProductDTO.setProductId(orderWorkRedistributeApply.getProductId());
                orderProductDTO.setProductShowType(BaseProductConsts.ERP_SHOW_TYPE);
                orderProductDTO.setNumber(product.getNumber());
                orderProductDTO.setBrandId(product.getBrandId());
                orderProductList.add(orderProductDTO);
                updateOrderDTO.setOrderProductList(orderProductList);
            } else {
                List<OrderProductDTO> orderProductList = Lists.newArrayList();
                OrderProductDTO orderProductDTO = new OrderProductDTO();
                orderProductDTO.setProductId(product.getProductId());
                orderProductDTO.setShowProductId(product.getShowProductId());
                orderProductDTO.setProductShowType(NumberUtil.isNullOrZero(product.getProductId()) ? BaseProductConsts.EC_SHOW_TYPE : BaseProductConsts.ERP_SHOW_TYPE);
                orderProductDTO.setNumber(product.getNumber());
                orderProductDTO.setBrandId(product.getBrandId());
                orderProductList.add(orderProductDTO);
                updateOrderDTO.setOrderProductList(orderProductList);
            }

            if (isAddressChanged) {
                updateOrderDTO.setLatitude(orderWorkRedistributeApply.getLatitude());
                updateOrderDTO.setLongitude(orderWorkRedistributeApply.getLongitude());
                updateOrderDTO.setStreetId(orderWorkRedistributeApply.getStreetId());
                updateOrderDTO.setStreet(orderWorkRedistributeApply.getStreet());
                updateOrderDTO.setCountyId(orderWorkRedistributeApply.getCountyId());
                updateOrderDTO.setAddress(orderWorkRedistributeApply.getAddress());
                if (NumberUtil.isNotNullOrZero(streetSource)) {
                    updateOrderDTO.setStreetSource(streetSource);
                }
            }

            updateOrderDTO.setOperatorId(orderWorkRedistributeApply.getApplyUserId());
            updateOrderDTO.setOperator(orderWorkRedistributeApply.getApplyUser());
            updateOrderDTO.setOperatorType(orderWorkRedistributeApply.getApplyUserType());
            updateOrderDTO.setOperateTime(DateUtil.getNow());

            // 如果修改订单会导致派单撤回允许派单撤回
            updateOrderDTO.setVerifyDistributeBack(Boolean.TRUE);
            // 设置为不发送到自动流转
            updateOrderDTO.setAutoFlow(Boolean.FALSE);
            // 更新工单
            zsNormalOrderBService.updateOrder(updateOrderDTO);
        } catch (OmsBaseException e) {
            log.error("更新订单数据失败：{}", e);
        }
    }

    /**
     * 发送转单求助信息
     *
     * @param redistributeMasterList
     * @param orderWork
     */
    private void pushRedistributeHelpAppMsg(List<RedistributeMasterBO> redistributeMasterList, OrderWorkRedistributeApplyBO orderWorkRedistributeApplyBO, OrderWork orderWork, OrderDetail orderDetail) {
        log.info("#pushRedistributeHelpAppMsg:{}", JSON.toJSONString(redistributeMasterList));
        if (CollectionUtil.isNullOrEmpty(redistributeMasterList)) {
            return;
        }
        String productInfo = orderWorkRedistributeApplyBO.getProductInfo();
        if (StringUtil.isBlank(productInfo)) {
            productInfo = orderDetail.getProductInfo();
        }
        JSONObject params = new JSONObject();
        params.put("product_name", productInfo);
        params.put("reason", orderWorkRedistributeApplyBO.getReason());
        params.put("redistribute_id", orderWorkRedistributeApplyBO.getRedistributeId());
        params.put("order_id", orderWork.getOrderId());
        params.put("work_id", orderWork.getWorkId());
        // 发送消息通知
        List<Integer> masterIdList = redistributeMasterList.stream().map(RedistributeMasterBO::getMasterId).collect(Collectors.toList());
        AppMessageDTO appMessageDTO = AppMessageDTO.builder()
                .orderId(orderWork.getOrderId())
                .plat(orderWork.getPlatWork())
                .masterIdList(masterIdList)
                .ruleId(MessageConsts.ZMN_MSG_RULEID_HELP_ORDER)
                .ruleCode(MessageConsts.ZMN_MSG_RULE_CODE_HELP_ORDER)
                .params(params.toJSONString())
                .build();
        messageSendBaseService.batchSendAndroidAndIOSMessage(appMessageDTO);

    }

    /**
     * 发送钉钉消息通知工程师主管
     *
     * @param auditBO
     */
    private Boolean sendDingTalkMessage(OrderWorkRedistributeAuditBO auditBO) {
        if (NumberUtil.isNullOrZero(auditBO.getMasterSupervisorId())
                || Objects.equals(auditBO.getMasterId(), auditBO.getMasterSupervisorId())) {
            return Boolean.FALSE;
        }
        ResponseDTO<String> responseDTO = null;
        try {
            responseDTO = engineerSupervisorListRemoteService.getDTalkIdBySupervisorId(auditBO.getMasterSupervisorId().intValue());
            log.info("改派申请[{}]查询员工钉钉id入参：【{}】，出参：【{}】", auditBO.getRedistributeId(), auditBO.getMasterSupervisorId(), JSON.toJSONString(responseDTO));
            if (!responseDTO.isSuccess() || StringUtil.isBlank(responseDTO.getData())) {
                log.error("改派申请待审核-发起钉钉消息失败-获取工程师主管钉钉id失败");
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            log.info("调用工程师接口失败：{}", e);
            return Boolean.FALSE;
        }

        // 钉钉消息
        SendMscDIO sendMscDIO = new SendMscDIO();
        sendMscDIO.setRuleId(MessageConsts.DINGTALK_MSG_RELEID_REDISTRIBUTE_AUDIT);
        sendMscDIO.setRuleCode(MessageConsts.ZMN_MSG_RULE_CODE_REDISTRIBUTE_AUDIT);
        DingTalkMessageDIO dingTaikParams = new DingTalkMessageDIO();
        sendMscDIO.setDingTalkParams(dingTaikParams);

        String applyUser = auditBO.getApplyUser();
        if (Objects.equals(auditBO.getApplyUserType(), GlobalConsts.OPERATE_USER_TYPE_MASTER)) {
            applyUser = applyUser + "工程师";
        }

        dingTaikParams.setDingTalkId(responseDTO.getData()); // 员工钉钉ID
        JSONObject params = new JSONObject();
        params.put("master_name", applyUser);
        params.put("order_id", auditBO.getOrderId());
        dingTaikParams.setParams(params.toJSONString());
        ZmnMcsSendUtil.send(sendMscDIO);
        return Boolean.TRUE;
    }

    /**
     * 设置审核日志参数
     *
     * @param auditBO
     * @param orderWorkRedistributeApply
     */
    private void setAuditLogParams(OrderWorkRedistributeAuditBO auditBO, OrderWorkRedistributeApply orderWorkRedistributeApply) {
        auditBO.setRedistributeOperatorLogRemark("");
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("审核结果：%s", RedistributeAuditStatusEnum.getEnumDesc(orderWorkRedistributeApply.getAuditStatus())));

        // 后台人员【核实的缘由】
        if (StringUtils.isNotBlank(auditBO.getAuditReason())) {
            sb.append("\n");
            sb.append(String.format("核实的改派缘由：%s", auditBO.getAuditReason()));
        }

        sb.append("\n虚假改派：");
        if (Objects.equals(GlobalConsts.YES, orderWorkRedistributeApply.getSpuriousStatus())) {
            sb.append("是");
        } else {
            sb.append("否");
        }

        sb.append("\n");
        sb.append(String.format("审核人：%s", orderWorkRedistributeApply.getAuditer()));

        // 审核备注
        if (StringUtils.isNotBlank(auditBO.getAuditRemark())) {
            sb.append("\n");
            sb.append(String.format("审核备注：%s", auditBO.getAuditRemark()));
        }
        auditBO.setRedistributeOperatorLogRemark(sb.toString());
    }
}
